package copyengine.ui.component.panel
{
	import copyengine.utils.GeneralUtils;
	import copyengine.utils.Random;

	import flash.display.Shape;
	import flash.geom.Rectangle;

	public final class CEScrollableVPanel extends CEVPanelBasic
	{
		/**
		 *<b>每次点击按钮滚动当前页面的百分比</b>
		 */
		private static const EACH_ONE_SCROLL_PERCENT:int = 20;

		private var panelWidth:int;
		private var panelHeight:int;
		private var panelScrollRect:Rectangle;

		private var eachScrollPercent:int;

		/**
		 *<b>记录当前VPanel是否已经滚动到了最下方</b>
		 * </br>在制作聊天窗VPanel时候,如果Layout时候已经在最下方则需要继续更新到最下方,
		 * 默认为True原因是在起始时候scrollPostion=0,所以在不存在Scroolbar的时候所有设置scrollpostion
		 * 请求都会return
		 */
		private var isAlreadyScrollToBottom:Boolean = true;

		public function CEScrollableVPanel(_panelWidth:int, _panelHeight:int, _vGap:int = 0, _eachScrollPercent:int = EACH_ONE_SCROLL_PERCENT)
		{
			panelWidth = _panelWidth;
			panelHeight = _panelHeight;
			eachScrollPercent = _eachScrollPercent;
			super(_vGap);
		}

		override protected function doInitialize():void
		{
			panelScrollRect = new Rectangle();
			panelScrollRect.x = 0;
			panelScrollRect.y = 0;
			panelScrollRect.width = panelWidth;
			panelScrollRect.height = panelHeight;
			refreshScrollRect();

			//在Panel底部垫一个透明的板子,用于相应MouseWheel事件
			var warpShape:Shape = new Shape();
			warpShape.graphics.beginFill(0, 0);
			warpShape.graphics.drawRect(0, 0, panelWidth, panelHeight);
			warpShape.graphics.endFill();
			panelBgContainer.addChild(warpShape);
		}

		override protected function onFinishedLaytoutSubElement():void
		{
			//重新设置一边ScrollPostion
			//如果当前已经滚动到最后，则默认继续往下滚动到最下方
			scrollPosition = isAlreadyScrollToBottom == true ? maxScrollValue : scrollPosition;
			scrollPosition = scrollPosition;
			if (hasEventListener(CEScrollableVPanelEvent.PANEL_SIZE_CHANGE))
			{
				var event:CEScrollableVPanelEvent = new CEScrollableVPanelEvent(CEScrollableVPanelEvent.PANEL_SIZE_CHANGE);
				this.dispatchEvent(event);
				event = null;
			}
		}

		public function set scrollPosition(_value:Number):void
		{
			if (_value != scrollPosition)
			{
				var expectScrollPositon:Number = GeneralUtils.normalizingValue(minScrollValue, _value, maxScrollValue);

				panelScrollRect.x = 0;
				panelScrollRect.y = expectScrollPositon;
				panelScrollRect.width = panelWidth;
				panelScrollRect.height = panelHeight;
				refreshScrollRect();
				//maxScrollValue==0表示当前VPanl还不能滚动
				isAlreadyScrollToBottom = expectScrollPositon == maxScrollValue || maxScrollValue == 0;
				if (hasEventListener(CEScrollableVPanelEvent.SCROLL_START))
				{
					var event:CEScrollableVPanelEvent = new CEScrollableVPanelEvent(CEScrollableVPanelEvent.SCROLL_START);
					event.expectScrollPositon = expectScrollPositon;
					this.dispatchEvent(event);
					event = null;
				}
			}
		}

		public function get scrollPosition():Number
		{
			return panelScrollRect.y;
		}

		public function get maxScrollValue():int
		{
			return Math.max(0, getVPanelRealHeight() - panelHeight);
		}

		public function get minScrollValue():int
		{
			return 0;
		}

		public function getLineScrollSize():int
		{
			return eachScrollPercent;
		}

		public function getPageScrollSize():int
		{
			return panelHeight;
		}

		public function scrollToNextOne():void
		{
			scrollPosition += panelHeight * eachScrollPercent * 0.01;
		}

		public function scrollToPrevOne():void
		{
			scrollPosition -= panelHeight * eachScrollPercent * 0.01;
		}

		public function scrollToNextPage():void
		{
			scrollPosition += panelHeight;
		}

		public function scrollToPrevPage():void
		{
			scrollPosition -= panelHeight;
		}

		public function scrollToStart():void
		{
			scrollPosition = minScrollValue;
		}

		public function scrollToEnd():void
		{
			scrollPosition = maxScrollValue;
		}

		private function refreshScrollRect():void
		{
			panelContainer.scrollRect = panelScrollRect;
		}

	}
}
